home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / slzwce.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.0 KB  |  162 lines

  1. /* Copyright (C) 1994, 1995, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: slzwce.c,v 1.2 2000/09/19 19:00:50 lpd Exp $ */
  20. /* Simple encoder compatible with LZW decoding filter */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "gdebug.h"
  23. #include "strimpl.h"
  24. #include "slzwx.h"
  25.  
  26. /* ------ Alternate LZWEncode filter implementation ------ */
  27.  
  28. /*
  29.  
  30.    The encoded data stream produced by this implementation of the LZWEncode
  31.    filter consists of a sequence of 9-bit data elements.  These elements are
  32.    packed into bytes in big-endian order, e.g. the elements
  33.  
  34.    100000000 001100001
  35.  
  36.    occurring at the very beginning of the data stream would be packed into
  37.    bytes as
  38.  
  39.    10000000 00011000 01......
  40.  
  41.    The first bit of each data element is a control bit.  If the control bit is
  42.    0, the remaining 8 bits of the data element are a data byte.  If the control
  43.    bit is 1, the remaining 8 bits of the data element define a control
  44.    function:
  45.  
  46.    1 00000000   synchronization mark, see below
  47.    1 00000001   end of data
  48.    1 xxxxxxxx   not used (all other values)
  49.  
  50.    The synchronization mark occurs at the beginning of the data stream, and at
  51.    least once every 254 data bytes thereafter.
  52.  
  53.    This format is derived from basic principles of data encoding (the use of a
  54.    separate flag bit to distinguish out-of-band control information from data
  55.    per se, and the use of a periodic synchronization mark to help verify the
  56.    validity of a data stream); it has no relationship to data compression.  It
  57.    is, however, compatible with LZW decompressors.  It produces output that is
  58.    approximately 9/8 times the size of the input.
  59.  
  60.  */
  61.  
  62. /* Define the special codes, relative to 1 << InitialCodeLength. */
  63. #define CODE_RESET 0
  64. #define CODE_EOD 1
  65. #define CODE_0 2        /* first assignable code */
  66.  
  67. /* Internal routine to put a code into the output buffer. */
  68. /* Let S = ss->code_size. */
  69. /* Relevant invariants: 9 <= S <= 15, 0 <= code < 1 << S; */
  70. /* 1 <= ss->bits_left <= 8; only the rightmost (8 - ss->bits_left) */
  71. /* bits of ss->bits contain valid data. */
  72. private byte *
  73. lzw_put_code(register stream_LZW_state * ss, byte * q, uint code)
  74. {
  75.     uint size = ss->code_size;
  76.     byte cb = (ss->bits << ss->bits_left) +
  77.     (code >> (size - ss->bits_left));
  78.  
  79.     if_debug2('W', "[w]writing 0x%x,%d\n", code, ss->code_size);
  80.     *++q = cb;
  81.     if ((ss->bits_left += 8 - size) <= 0) {
  82.     *++q = code >> -ss->bits_left;
  83.     ss->bits_left += 8;
  84.     }
  85.     ss->bits = code;
  86.     return q;
  87. }
  88.  
  89. /* Initialize LZW-compatible encoding filter. */
  90. private int
  91. s_LZWE_reset(stream_state * st)
  92. {
  93.     stream_LZW_state *const ss = (stream_LZW_state *) st;
  94.  
  95.     ss->code_size = ss->InitialCodeLength + 1;
  96.     ss->bits_left = 8;
  97.     /* Force the first code emitted to be a reset. */
  98.     ss->next_code = (1 << ss->code_size) - 2;
  99.     return 0;
  100. }
  101. private int
  102. s_LZWE_init(stream_state * st)
  103. {
  104.     stream_LZW_state *const ss = (stream_LZW_state *) st;
  105.  
  106.     ss->InitialCodeLength = 8;
  107.     ss->table.encode = 0;
  108.     return s_LZWE_reset(st);
  109. }
  110.  
  111. /* Process a buffer */
  112. private int
  113. s_LZWE_process(stream_state * st, stream_cursor_read * pr,
  114.            stream_cursor_write * pw, bool last)
  115. {
  116.     stream_LZW_state *const ss = (stream_LZW_state *) st;
  117.     register const byte *p = pr->ptr;
  118.     const byte *rlimit = pr->limit;
  119.     register byte *q = pw->ptr;
  120.     byte *wlimit = pw->limit;
  121.     int status = 0;
  122.     int signal = 1 << (ss->code_size - 1);
  123.     uint limit_code = (1 << ss->code_size) - 2;        /* reset 1 early */
  124.     uint next_code = ss->next_code;
  125.  
  126.     while (p < rlimit) {
  127.     if (next_code == limit_code) {    /* Emit a reset code. */
  128.         if (wlimit - q < 2) {
  129.         status = 1;
  130.         break;
  131.         }
  132.         q = lzw_put_code(ss, q, signal + CODE_RESET);
  133.         next_code = signal + CODE_0;
  134.     }
  135.     if (wlimit - q < 2) {
  136.         status = 1;
  137.         break;
  138.     }
  139.     q = lzw_put_code(ss, q, *++p);
  140.     next_code++;
  141.     }
  142.     if (last && status == 0) {
  143.     if (wlimit - q < 2)
  144.         status = 1;
  145.     else {
  146.         q = lzw_put_code(ss, q, signal + CODE_EOD);
  147.         if (ss->bits_left < 8)
  148.         *++q = ss->bits << ss->bits_left;    /* final byte */
  149.     }
  150.     }
  151.     ss->next_code = next_code;
  152.     pr->ptr = p;
  153.     pw->ptr = q;
  154.     return status;
  155. }
  156.  
  157. /* Stream template */
  158. const stream_template s_LZWE_template = {
  159.     &st_LZW_state, s_LZWE_init, s_LZWE_process, 1, 2, NULL,
  160.     s_LZW_set_defaults, s_LZWE_reset
  161. };
  162.